﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using Zeus.Thunder.Core;

namespace SharpTrainer.Other
{
    class ZipFileTest2 : ITestCase
    {
        public void Run()
        {
            //string dir = AppDomain.CurrentDomain.BaseDirectory;
            //string zipFilePath = "D://ZipTest//zipfile.zip";

            string zipFilePath = Path.Combine(Path.GetFullPath("..\\..\\"), "Other\\zipfile.zip");

            string[] fileArr1 = { "3.txt", "1.txt", "2.txt"};
            bool hasFiles = HasFilesInZip(fileArr1, zipFilePath);
            Console.WriteLine("Zip File: {0} has the files: {1}", zipFilePath, hasFiles);

            string[] fileArr2 = { "3.txt", "5.txt", "2.txt" };
            hasFiles = HasFilesInZip(fileArr2, zipFilePath);
            Console.WriteLine("Zip File: {0} has the files: {1}", zipFilePath, hasFiles);

            string[] fileArr3 = { "1.txt", "3.txt" };
            hasFiles = HasFilesInZip(fileArr3, zipFilePath);
            Console.WriteLine("Zip File: {0} has the files: {1}", zipFilePath, hasFiles);
        }

        private bool HasFilesInZip(string[] files, string zipFile)
        {
            bool match = true;

            for (int i=0; i< files.Length; i++)
            {
                if (!HasFileInZip(files[i], zipFile))
                {
                    match = false;
                    break;
                }
            }

            return match;
        }

        private bool HasFileInZip(string fileName, string zipFile)
        {
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile)))
            {
                ZipEntry theEntry;

                while ((theEntry = s.GetNextEntry()) != null)
                {
                    if (fileName == theEntry.Name)
                    {
                        return true;
                    }
                }

                return false;
            }
        }
    }
}
